package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayBestService {
    @Autowired
    public UserService userService;
    @Autowired
    private RecommendUserService recommendUserService;
    @Autowired
    private UserInfoService userInfoService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;
    public TodayBest queryTodayBest(/*String token*/) {
        //无需校验直接获取
        User user= UserThreadLocal.get();
        /* //校验token是否有效，通SSO的接口校验
        User user= this.userService.queryUserByToken(token);
        if(null==user){
            //token非法或已过期
            return null;
        }*/
        //查询推荐用户（今日佳人）
        TodayBest todayBest=  this.recommendUserService.queryTodayBest(user.getId());
        if(null==todayBest){
            //给出默认推荐用户
            todayBest=new TodayBest();
            todayBest.setId(defaultUser);
            //固定缘分值
            todayBest.setFateValue(80L);
        }
        //补全个人信息
        UserInfo userInfo =  this.userInfoService.queryUserInfoByUserId(todayBest.getId());
          if(null==userInfo){
            return null;
          }
          todayBest.setAvatar(userInfo.getLogo());
          todayBest.setNickname(userInfo.getNickName());
          todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
          todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
          todayBest.setAge(userInfo.getAge());
          return todayBest;
    }

    public PageResult queryRecommendation(/*String token,*/RecommendUserQueryParam queryParam) {
        //
        User user= UserThreadLocal.get();
        /* //校验token是否有效，通SSO的接口校验
        User user= this.userService.queryUserByToken(token);
        if(null==user){
            //token非法或已过期
            return null;
        }*/
        PageResult pageResult=new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        PageInfo<RecommendUser> pageInfo = this.recommendUserService.queryRecommendUserList(user.getId(), queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if(CollectionUtils.isEmpty(records)){
            //没有查询到推荐的用户列表
            return pageResult;
        }
        //填充个人信息

        //收集推荐用户的id
       /* Set<Long> userIds=new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }*/
       Map<Long,Double> userIds= new HashMap<>();
        for (RecommendUser record : records) {
            userIds.put(record.getUserId(),record.getScore());
        }
        QueryWrapper<UserInfo> queryWrapper =new QueryWrapper();
        //用户id参数
        queryWrapper.in("user_id",userIds.keySet());
        if(StringUtils.isEmpty(queryParam.getGender())){
            //需要性别参数查询
           // queryWrapper.eq("sex",StringUtils.equals(queryParam.getGender(),"man")? 1:2);
        }
        if(StringUtils.isEmpty(queryParam.getCity())){
            //需要城市参数查询
            //queryWrapper.like("city",queryParam.getCity());
        }
        if(queryParam.getAge()!=null ){
            //设置年龄参数，条件：小于等于
           // queryWrapper.le("age",queryParam.getAge());
        }
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
        if(CollectionUtils.isEmpty(userInfoList)){
        //没有查询到用户的基本信息
            return  pageResult;
        }
        //补全推荐朋友基本信息
        List<TodayBest> todayBests=new ArrayList<>();
        TodayBest todayBest=null;
        for (UserInfo userInfo : userInfoList) {
            todayBest=new TodayBest();

            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
            todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
            todayBest.setAge(userInfo.getAge());
            //缘分值
            /*for (RecommendUser record : records) {
                if(record.getUserId().longValue()==userInfo.getUserId().longValue()){
                    double score = Math.floor(record.getScore());//取整,98.2 -> 98
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }*/
            //缘分值
            Double score=Math.floor(userIds.get(userInfo.getUserId()));
            todayBest.setFateValue(score.longValue());
            todayBests.add(todayBest);
        }
        //按照缘分倒叙排序
        Collections.sort(todayBests,((o1, o2) -> new Long(o2.getFateValue()-o1.getFateValue()).intValue()));
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
